import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      if (itemStr) {
        this.findOrderList();
      }
    },
    async findOrderList() {
      action.commonChange('main.isLoading', true);
      let {pageNum, orderList, curNow} = getData().main;
      //订单状态；0(预约成功，待处理) 2(已受理，等待收件) 21(已揽收) 3(已开单) 4(受理失败，无法取件，原因在remarks) 5（已签收）
      let status = curNow == 0 ? null : curNow == 1 ? 2 : curNow == 2 ? 21 : 3; //订单状态
      const params = {
        pageNum,
        pageSize: 10,
        whaleOrderStatus: status,
      };
      let result = await api.OnecategoryController.findWhaleorderOrder(params);
      if (result.code == 'K-000000') {
        this.commonChange('main.totalPage', result.context.totalPages);
        if (pageNum == 0) {
          action.commonChange('main.orderList', result.context.content);
        } else {
          let newList = orderList.concat(result.context.content);
          action.commonChange('main.orderList', newList);
        }
      }
      this.commonChange('main.isLoading', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageDMeituanOrders'),
  };
}

//create by moon https://github.com/creasy2010/moon
